//
//  XYEmoticon.swift
//  WeiboSourceCode
//
//  Created by 周星宇 on 16/1/7.
//  Copyright © 2016年 cn.weibo. All rights reserved.
//

import UIKit

class XYEmoticon: NSObject,NSCoding {
    
    /// 表情的描述
    var chs: String?
    /// 表情的图片名字
    var png: String?
    /// 表情类型，如果是图片表情，type 为 "0"，emoji 表情 type 为 "1"
    var type: String?
    /// emoji表情的字符串
    var code: String?
    /// 图片是放在哪个路径下的
    var path: String?
    
    // 返回表情图片的全路径
    var fullPath: String {
        return "\(path!)/\(png!)"
    }
    /// 判断当前表情模型是否是Emoji表情
    var isEmoji: Bool {
        return type == "1"
    }
    
    init(dict: [String: AnyObject]) {
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {}
    
    
    override var description: String {
        let keys = ["chs", "code", "type"]
        return dictionaryWithValuesForKeys(keys).description
    }
    // 解档
    required init?(coder decoder: NSCoder) {
        
        chs = decoder.decodeObjectForKey("chs") as? String
        png = decoder.decodeObjectForKey("png") as? String
        type = decoder.decodeObjectForKey("type") as? String
        code = decoder.decodeObjectForKey("code") as? String
        path = decoder.decodeObjectForKey("path") as? String
    }
    // 归档
    func encodeWithCoder(enCoder: NSCoder) {
        enCoder.encodeObject(chs, forKey: "chs")
        enCoder.encodeObject(png, forKey: "png")
        enCoder.encodeObject(type, forKey: "type")
        enCoder.encodeObject(code, forKey: "code")
        enCoder.encodeObject(path, forKey: "path")
        
    }
    override func isEqual(object: AnyObject?) -> Bool {
        guard let emoticon = object as? XYEmoticon else{
            return super.isEqual(object)
        }
        // 判断是否是恶魔及
        if self.isEmoji == emoticon.isEmoji { //说明类型相同
            if self.isEmoji {
                if self.code! == emoticon.code! {
                    return true
                }
            }else{
                if self.chs! == emoticon.chs! {
                    return true
                }
            }
        }
        return false
    }
}

